/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author AhmedSalama
 */
public class shopping extends HttpServlet {
   private static int i=0;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet shopping</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet shopping at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>shopping</title></head>");
        out.println("<body>");
      out.println("<form method='post'>");
        out.println("<select  name=\"choose\">");
        out.println("<option value=\"g\"> Google</option>");
        out.println("<option value=\"y\">Yahoo</option>" );
        out.println("<option value=\"b\">Bing</option>");
        out.println("</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        out.println("<input type=\"submit\" name=\"add\" value=\"Add\"/>");
        out.println("</form>");
        HttpSession ses=request.getSession(false);
        if(ses!=null){
      out.println("<form method='post'>");

      for(int y=0;y<(Integer)ses.getAttribute("i");y++){
      out.println(ses.getAttribute("it"+y));
      }
      out.println("<input type=\"submit\" name=\"Check Out\" value=\"Search\"/>");
      out.println("</form>");
    out.println("</body></html>");
        out.close();
    } 
    }
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
              HttpSession ses=request.getSession();

  String choose = request.getParameter("choose");

      switch (choose.charAt(0)){
      case 'g':
        ses.setAttribute("it"+i,"Google");


          break;
        case 'b':
        ses.setAttribute("it"+i,"bing");
          break;
        case 'y':
        ses.setAttribute("it"+i,"Yahoo");
            break;
      }
  i++;
  ses.setAttribute("i",new Integer(i));
        RequestDispatcher ree;
        ree = request.getRequestDispatcher("shopping");
        ree.forward(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
